package com.qding.park.common.utils.qiniu;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.processing.OperationManager;
import com.qiniu.processing.Pipe;
import com.qiniu.processing.image.BasicOp;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.storage.model.FileListing;
import com.qiniu.util.Auth;
import com.qiniu.util.Img2FileUtils;

/**
 * 
 * 项目名称：qiniucloud-test 类名称：ImgService 类描述： 以下是私有空间的图片处理服务 创建人：lijun
 * 创建时间：2015年3月19日 下午2:39:36 修改人：lijun 修改时间：2015年3月19日 下午2:39:36 修改备注：
 * 
 * @version
 * 
 */
public class ImgServiceInPrivate extends BaseService {
	private static final Logger log = Logger.getLogger(ImgServiceInPrivate.class);
	private String accessKey;
	private String secretKey;
	private String bucketName;
	private String domain;
	private Auth auth;
	private Long token_expire;
	private UploadManager upManager; // 文件上传
	private BucketManager bucketManager; // 文件管理
	private OperationManager opm; // 私有空间 文件下载和处理
	private final Properties properties = new Properties();

	public ImgServiceInPrivate(String config_path) throws FileNotFoundException, IOException {
		if (auth == null || upManager == null || bucketManager == null || opm == null) {
			init(config_path);
		}
	}

	public ImgServiceInPrivate(String accessKey, String secretKey, String bucketName, String domain,
			Long token_expire) {
		if (auth == null || upManager == null || bucketManager == null || opm == null) {
			this.accessKey = accessKey;
			this.secretKey = secretKey;
			this.bucketName = bucketName;
			this.domain = domain;
			auth = Auth.create(accessKey, secretKey);
			upManager = new UploadManager();
			bucketManager = new BucketManager(auth);
			super.setBucketName(bucketName);
			super.setBucketManager(bucketManager);
			String tokenExpire = properties.getProperty("token_expire");
			if (tokenExpire != null) { // 私有空间 下载管理类
				this.token_expire = Long.valueOf(tokenExpire);
				opm = new OperationManager(domain, auth, token_expire);
			}
		}

	}

	private void init(String config_path) throws FileNotFoundException, IOException {
		File file = new File(config_path);
		Reader reader = null;
		if (file.exists()) {
			try {
				reader = new FileReader(config_path);
			} catch (FileNotFoundException e) {
				reader = new InputStreamReader(ImgServiceInPrivate.class.getResourceAsStream("/" + config_path));
			}
		} else {
			reader = new InputStreamReader(ImgServiceInPrivate.class.getResourceAsStream("/" + config_path));
		}

		properties.load(reader);
		reader.close();
		this.accessKey = properties.getProperty("accessKey");
		this.secretKey = properties.getProperty("secretKey");
		this.bucketName = properties.getProperty("bucketName");
		this.domain = properties.getProperty("domain");
		auth = Auth.create(accessKey, secretKey);
		upManager = new UploadManager();
		bucketManager = new BucketManager(auth);
		super.setBucketName(bucketName);
		super.setBucketManager(bucketManager);
		String tokenExpire = properties.getProperty("token_expire");
		if (tokenExpire != null) { // 私有空间 下载管理类
			this.token_expire = Long.valueOf(tokenExpire);
			opm = new OperationManager(domain, auth, token_expire);
		}
	}

	// *************************************
	// 以下为私有空间的上传和下载方法，默认为私有空间**************************************
	/**
	 * 从服务器端上传图片到云端
	 * 
	 * @param filePath
	 * @param key
	 * @return Response
	 * @throws QiniuException
	 */
	public Response UploadImg(String filePath, String key) throws QiniuException {
		String token = auth.uploadToken(bucketName);
		Response r = upManager.put(filePath, key, token);
		return r;
	}

	public Response UploadImg(final byte[] data, final String key) throws QiniuException {
		String token = auth.uploadToken(bucketName);
		Response r = upManager.put(data, key, token);
		return r;
	}

	/**
	 * 获取下载原图的url
	 * 
	 * @param key
	 * @return
	 * @throws QiniuException
	 */
	public String GetDownloadImgUrl(String key) throws QiniuException {
		return opm.buildUrl(key, null);
	}

	/**
	 * 获取下载下载缩略图的url
	 * 
	 * @param key
	 * @param processMode
	 * @param width
	 * @param height
	 * @return
	 * @throws QiniuException
	 */
	public String GetDownloadThumbnailUrl(String key, int processMode, int width, int height) throws QiniuException {
		BasicOp op = new BasicOp(processMode);
		op.width(width);
		op.height(height);
		return opm.buildUrl(key, Pipe.create().append(op));
	}

	/**
	 * 从七牛云端下载原图片到本地服务器
	 * 
	 * @param filePath
	 * @param key
	 * @return Response
	 * @throws QiniuException
	 */
	public Response DownloadImg(String filePath, String key) throws QiniuException {
		Response r = opm.get(key);
		Img2FileUtils.saveStreamToImg(r.body(), filePath);
		return r;
	}

	/**
	 * 从七牛下载缩略图到本地服务器
	 * 
	 * @param filePath
	 * @param key
	 * @param processMode
	 * @param width
	 * @param height
	 * @return
	 * @throws QiniuException
	 */
	public Response DownloadThumbnail(String filePath, String key, int processMode, int width, int height)
			throws QiniuException {
		BasicOp op = new BasicOp(processMode);
		op.width(width);
		op.height(height);
		Response r = opm.get(key, op);
		Img2FileUtils.saveStreamToImg(r.body(), filePath);
		return r;
	}

	public UploadManager getUpManager() {
		return upManager;
	}

}
